/**
 * Author : Fengyuan(Franklin) Zhang
 * Date : 2018/12/27
 * Description : Task
 */


var mongoose = require('./mongooseModel');
var BaseModel = require('./baseModel');

var Task = function (task) {
    this.t_inputs = null;
    this.t_outputs = null;
    this.t_user = null;
    this.t_status = '';
    this.t_server = null;
    this.t_msrid = '';
    this.t_type = 1;
    this.t_note = '';
    this.t_pid = '';
    if(task && task.t_inputs){
        this.t_inputs = task.t_inputs;
    }
    if(task && task.t_outputs){
        this.t_outputs = task.t_outputs;
    }
    if(task && task.t_user){
        this.t_user = task.t_user;
    }
    if(task && task.t_status){
        this.t_status = task.t_status;
    }
    if(task && task.t_server){
        this.t_server = task.t_server;
    }
    if(task && task.t_msrid){
        this.t_msrid = task.t_msrid;
    }
    if(task && task.t_type){
        this.t_type = task.t_type;
    }
    if(task && task.t_note){
        this.t_note = task.t_note;
    }
    if(task && task.t_pid){
        this.t_pid = task.t_pid;
    }
    return this;
}

Task.__proto__ = BaseModel;
module.exports = Task;

var taskSchame = new mongoose.Schema({
    t_inputs : Array,
    t_datetime : Date,
    t_outputs : Array,
    t_user : mongoose.Schema.Types.Mixed,
    t_status : String,
    t_server : mongoose.Schema.Types.Mixed,
    t_msrid : String,
    t_type : Number,
    t_note : String,
    t_pid: String
},{collection:'task'});
var taskModel = mongoose.model('task', taskSchame);
Task.baseModel = taskModel;
Task.modelName = "Task Model";

Task.getByServerAndStatus = function(server_id, status,type, callback){
    this.getByWhere({t_server: server_id, t_status: status, t_type: type}, this.returnFunction(callback, 'Error in getting tasks by server id and inited status'));
}

Task.getAllByStatus = function(status, callback){
    this.getByWhere({t_status: status}, this.returnFunction(callback, 'Error in getting all tasks with status'));
}

Task.getAllByServerAndStatus = function(server_id, callback){
    this.getByWhere({t_server: server_id, t_status: {$regex:"[Inited|Started]"}}, this.returnFunction(callback, 'Error in getting tasks by server id and inited status'));
}

Task.getAllByServerWithPidAndStatus = function(server_id,pid, status, callback){
    this.getByWhere({t_server: server_id, t_pid: pid, t_status: status}, this.returnFunction(callback, 'Error in getting tasks by server id and status'));
}

Task.getAllInitedTasks = function(callback){
    this.getByWhere({t_status: "Inited"}, this.returnFunction(callback, 'Error in getting tasks by server id and status'));
}

Task.getAllInitedAndStartedTasks = function(callback){
    this.getByWhere({t_status:{$in: ["Inited","Started"]}}, this.returnFunction(callback, 'Error in getting tasks by status'));
}

Task.getAllFinishedTasksByPid = function (pid,callback) {
    this.getByWhere({t_pid: pid, t_status: "Finished"}, this.returnFunction(callback, 'Error in getting tasks by pid'));
}
